package com.example.lab04.controller;

import com.example.lab04.entity.Member;
import com.example.lab04.service.MemberService;
import com.example.lab04.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("member")
public class MemberController extends BaseController {

    private final MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping("queryAll")
    @ResponseBody
    private List<Member> queryAll() {
        return memberService.queryAll();
    }

    @PostMapping("create")
    @ResponseBody
    private boolean create(@RequestPart MultipartFile file, @RequestPart Member member) throws IOException {
        // todo 判断文件 文件大小 ...
        member.setAvatar(UploadUtil.savePicture(file, "D:/lab04/avatar"));
        memberService.create(member);
        return true;
    }

    @PostMapping("queryByAccountAndPassword")
    @ResponseBody
    private Member queryByAccountAndPassword(@RequestBody Map<String, String> map) {

        Member member = memberService.queryOne("queryByAccountAndPassword", map);
        session.setAttribute("member", member);
        return member;
    }
}
